# -*- coding: utf-8 -*-
"""
Created on Sun Jun 28 00:02:58 2020

@author: Administrator
"""

from socket import *
import time

import struct


def bytesToFloat(h1, h2, h3, h4):
    ba = bytearray()
    ba.append(h1)
    ba.append(h2)
    ba.append(h3)
    ba.append(h4)
    return struct.unpack("!f", ba)[0]


def floatToBytes(f):
    bs = struct.pack("f", f)
    return (bs[3], bs[2], bs[1], bs[0])


def print_hex(bytes):
    l = [hex(int(i)) for i in bytes]
    print(" ".join(l))


def isequal_bytes(bytes1, bytes2):
    return True


# answer = bytearray(3+238+2)
# answer[0] = 0x01
# answer[1] = 0x03
# answer[2] = 0xee
# answer[2+238+1] = 0
# answer[2+238+2] = 0
# for x in range(3, 3+238):
#    answer[x]= x-2
# print_hex(answer)

# 创建socket
tcp_client_socket = socket(AF_INET, SOCK_STREAM)

# 目的信息
# server_ip = input("请输入服务器ip:")
# 本地连接
server_ip = "127.0.0.1"
# 云链接
# server_ip = "122.51.56.204"
# server_port = int(input("请输入服务器port:"))
server_port = int("4096")

b = struct.pack("f", 123.12345)
for i in b:
    print(i)

# 链接服务器
while True:
    try:
        tcp_client_socket.connect((server_ip, server_port))
        break
    except ConnectionRefusedError as e:
        print(e.strerror)
        #        x = input("reconnect")
        time.sleep(5)

# 接收对方发送过来的数据，最大接收1024个字节
recvData = tcp_client_socket.recv(1024)
print('接收到的数据为:', recvData.decode('gbk'))

# 提示用户输入数据
# send_data = input("请输入要发送的数据：")
send_data = "123456"

# tcp_client_socket.send(send_data.encode("gbk"))
tcp_client_socket.send(send_data.encode())
print('数据已发送')

while True:

    # 接收对方发送过来的数据，最大接收1024个字节
    try:
        # recvData = tcp_client_socket.recv(1024)
        # 对方断开连接如何处理
        # print('接收到的数据为:', recvData.decode('gbk'))
        # print('接收到的数据为:', recvData.decode('gbk'))
        # print_hex(recvData)
        # 6042个0
        answer = bytearray(6042)

        # 长度6040，前2字节，x1798，高位在前
        answer[0] = 0x17
        answer[1] = 0x98

        bytes_of_float = floatToBytes(1.567)

        print("bytes_of_float=" + str(bytes_of_float))

        answer[2] = bytes_of_float[0]
        answer[3] = bytes_of_float[1]
        answer[4] = bytes_of_float[2]
        answer[5] = bytes_of_float[3]

        answer[2 + 0xb56 * 2 + 1] = 1  # 1通道点数
        answer[2 + 0xb57 * 2 + 1] = 1  # 2通道点数

        '''   
#        startAddress = int(recvData[2])<<8 + int(recvData[3]);
#        dataLength = int(recvData[4])<<8 + int(recvData[5]);
        startAddress = (recvData[2]<<8)+ recvData[3]
        dataLength = (recvData[4]<<8) + recvData[5]
        print('startAddress:0x%x'%startAddress)
        print('dataLength:0x%x'%dataLength)
        #一通道只数
        if recvData[2]== 0xb and recvData[3]==0x56 :
            #上线
            #answer = b'\x01\x03\x02\x00\x03\x00\x00'
            #下线
            answer = b'\x01\x03\x02\x00\x00\x00\x00'
        #二通道只数
        elif recvData[2] == 0xb and recvData[3]==0x57 :
            #上线
            #answer = b'\x01\x03\x02\x00\x02\x00\x00'
            #下线
            answer = b'\x01\x03\x02\x00\x00\x00\x00'
        elif startAddress == 0 :
            #3 Points, 12 bytes data
            answer = b'\x01\x03\x0c\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x00\x00'
        elif startAddress == 0x100 :
            #2 Points, 8 bytes data
            answer = b'\x01\x03\x08\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00'
        elif startAddress == 0x200 :
            #3 Points, 12 bytes data
            answer = b'\x01\x03\x0c\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x00\x00'
        elif startAddress == 0x300 :
            #2 Points, 8 bytes data
            answer = b'\x01\x03\x08\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00'
        elif startAddress == 0x400 :
            #3 Points, 12 bytes data
            answer = b'\x01\x03\x0c\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x00\x00'
        elif startAddress == 0x500 :
            #2 Points, 8 bytes data
            answer = b'\x01\x03\x08\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00'
        elif startAddress == 0x600 :
            #3 Points, 12 bytes data
            answer = b'\x01\x03\x0c\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x00\x00'
        elif startAddress == 0x700 :
            #2 Points, 8 bytes data
            answer = b'\x01\x03\x08\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00'
        elif startAddress == 0xa00 :
            #3 Points, 6 bytes data
            answer = b'\x01\x03\x06\x01\x02\x03\x04\x05\x06\x00\x00'
        elif startAddress == 0xa80 :
            #2 Points, 2 bytes data
            answer = b'\x01\x03\x04\x01\x02\x03\x04\x00\x00'
        elif startAddress == 2900 and dataLength > 4:
            answer = bytearray(3+240+2)
            answer[0] = 0x01
            answer[1] = 0x03
            answer[2] = 0xf0
            answer[2+240+1] = 0
            answer[2+240+2] = 0
            for x in range(3, 3+240):
                answer[x]= x-2
            print('发送的数据为:')
            print_hex(answer)
        elif startAddress > 2900 and dataLength == 4:
            answer = bytearray(3+4+2);
            answer[0] = 0x01
            answer[1] = 0x03
            answer[2] = 0x04
            answer[3] = 0x3f
            answer[4] = 0x80
            answer[5] = 0x00
            answer[6] = 0x00
            answer[7] = 0
            answer[8] = 0
        elif startAddress > 2900 and dataLength == 2:
            answer = bytearray(3+2+2);
            answer[0] = 0x01
            answer[1] = 0x03
            answer[2] = 0x02
            answer[3] = 0x01
            answer[4] = 0x02
            answer[5] = 0
            answer[6] = 0
        '''

        # answer = b'\x01\x03\x02' + b'\x00\x03\x00\x00'
        # print_hex(answer1)
        # tcp_client_socket.send(b'\x01\x03\x02\x00\x03\x00\x00')
        tcp_client_socket.send(answer)
    except ConnectionResetError as e:
        print(e.strerror)
        break

# 关闭套接字
tcp_client_socket.close()
# ————————————————
# 版权声明：本文为CSDN博主「Junieson」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
# 原文链接：https://blog.csdn.net/i6223671/java/article/details/100052423
